/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co

 */
package co.yixiang.tools.rest;

import co.yixiang.logging.aop.log.Log;
import co.yixiang.tools.domain.AliossConfig;
import co.yixiang.tools.domain.AliossContent;
import co.yixiang.tools.domain.QiniuConfig;
import co.yixiang.tools.domain.QiniuContent;
import co.yixiang.tools.service.AliOSSService;
import co.yixiang.tools.service.QiNiuService;
import co.yixiang.tools.service.dto.AliOSSQueryCriteria;
import co.yixiang.tools.service.dto.QiniuQueryCriteria;
import co.yixiang.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云oss存储
 * @author 闫愿望
 * @date 2020/12/27 15:16
 */
@Slf4j
@RestController
@RequestMapping("/api/aliOSSContent")
@Api(tags = "工具：阿里云存储管理")
public class AliOSSController {


    private final AliOSSService aliOSSService;



    public AliOSSController(AliOSSService aliOSSService) {
        this.aliOSSService = aliOSSService;
    }

    @GetMapping(value = "/config")
    public ResponseEntity<Object> get(){
        return new ResponseEntity<>(aliOSSService.find(), HttpStatus.OK);
    }

    @Log("配置阿里云存储")
    @ApiOperation("配置阿里云存储")
    @PutMapping(value = "/config")
    public ResponseEntity<Object> emailConfig(@Validated @RequestBody AliossConfig aliossConfig){
        aliOSSService.update(aliossConfig);
        aliOSSService.update(aliossConfig.getType());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, AliOSSQueryCriteria criteria) throws IOException {
        aliOSSService.downloadList(aliOSSService.queryAll(criteria), response);
    }

    @Log("查询文件")
    @ApiOperation("查询文件")
    @GetMapping
    public ResponseEntity<Object> getRoles(AliOSSQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(aliOSSService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("上传文件")
    @ApiOperation("上传文件")
    @PostMapping
    public ResponseEntity<Object> upload(@RequestParam MultipartFile file,Integer fileType,Long productId){

        AliossContent ailiossContent = aliOSSService.upload(file,aliOSSService.find());
        Map<String,Object> map = new HashMap<>(3);
        map.put("id",ailiossContent.getId());
        map.put("errno",0);
        map.put("data",new String[]{ailiossContent.getUrl()});
//        map.put("userId",ailiossContent.getUserId());
//        map.put("fileType",ailiossContent.getFileType());
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @Log("同步阿里云数据")
    @ApiOperation("同步阿里云数据")
    @PostMapping(value = "/synchronize")
    public ResponseEntity<Object> synchronize(Integer fileType,Long productId){
        aliOSSService.synchronize(aliOSSService.find(),SecurityUtils.getUserId(),fileType,productId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("下载文件")
    @ApiOperation("下载文件")
    @GetMapping(value = "/download/{id}")
    public ResponseEntity<Object> download(@PathVariable Long id){
        Map<String,Object> map = new HashMap<>(1);
        map.put("url", aliOSSService.download(aliOSSService.findByContentId(id),aliOSSService.find()));
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @Log("删除文件")
    @ApiOperation("删除文件")
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Object> delete(@PathVariable Long id){
        aliOSSService.delete(aliOSSService.findByContentId(id),aliOSSService.find());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("删除多张图片")
    @ApiOperation("删除多张图片")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        aliOSSService.deleteAll(ids, aliOSSService.find());
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
